//初始化树和列表高度
var docH = $(window).height();
$("body").height(docH).css('overflow','auto');

//标签下拉框，改变事件
function selectChange(obj){
	var dname =$(obj).find("option:selected").attr("dName");
	$(obj).prev("input").val(dname);
}


function submit(){
	layer.confirm("是否保存",{icon: 3, title:'保存确认'},function(index){
		layer.close(index);
		save();
	});
}

//保存
function save(){
	
	//检查是否有处于编辑的中的行未提交
	if(checkEditCount() != 0 ){
		layer.msg("请确认正在编辑的资质信息");
		return ;
	}
	
	//验证公司信息
	if(!checkCompayInfo()){
		return ;
	}
	//验证是否输入资质
	if(!checkCertifiate()){
		layer.msg("必须录入资质信息",{time:3000});
		return ;
	}
	
	//验证其他信息
	if(!checkOtherInfo()){
		return ;
	}
	
	$.ajax({
		url:rootpath+"/certificate/saveOrUpdate.shtml",
		type:"post",
		data:{
			updateObjs:validateData(updateObjs),
			addObjs:validateData(addObjs),
			deleteIds:deleteIds.join(","),
			checkReportAdd:validateData(checkReportAdd),
			checkReportDel:checkReportDel.join(","),
			certificateCompany:validateData(getFormData($("#companyForm").serializeArray())),
			certificateGood:validateData(getFormData($("#otherForm").serializeArray())),
		},
		dataType:"json",
		success:function(data){
			if(data.status){
				layer.alert(data.info,function(index){
					window.location.href=rootpath+"/certificate/list.shtml";
				});
			}else{
				layer.alert(data.info);
				return ;
			}
		},
		error:function(data){
		}
	});
	
}

//格式化表单数据
function getFormData(formSerializeArray){
	var obj = {};
	var isPush = false;
	$.each(formSerializeArray,function(){
		if(this.value == null || this.value==""){
			return 0;
		}
		obj[this.name] = this.value;
		isPush =true;
	});
	if(!isPush){
		return;
	}
	return obj;
}

//对象转json
function validateData(obj){
	if(obj == null || obj == ""){
		return;
	}else{
		return JSON.stringify(obj)
	}
}

//验证公司信息
function checkCompayInfo(){
	var $comForm = $("#companyForm");
	//统一信用代码验证
	if(checkNull($comForm.find("input[name='cfeComNo']"))){
		if(!checkNumAndLetter($comForm.find("input[name='cfeComNo']"))){
			layer.msg("统一信用代码只能包含数字或字母");
			return false;
		};
		/* if(!checkLengthEqual($comForm.find("input[name='cfeComName']"),18)){
			layer.msg("统一信用代码长度必须等于18位");
			return false;
		}; */
	}; 
	
	//公司名称验证
	if(!checkNull($comForm.find("input[name='cfeComName']"))){
		layer.msg("请输入公司名称");
		return false;
	};
	if(!checkLength($comForm.find("input[name='cfeComName']"),32)){
		layer.msg("公司名称长度不能超过32个");
		return false;
	};
	
	//注册资本
	if(!checkNull($comForm.find("input[name='cfeComRegCap']"))){
		layer.msg("请输入注册资本");
		return false;
	};
	
	if(!checkNumPorint($comForm.find("input[name='cfeComRegCap']"))){
		layer.msg("注册资本只能由整数或小数组成");
		return false;
	}
	
	//企业地址
	if(!checkNull($comForm.find("input[name='cfeComAddress']"))){
		layer.msg("请输入企业地址");
		return false;
	};
	
	//所在城市
	if(!checkNull($comForm.find("input[name='cfeComArea']"))){
		layer.msg("请输入所在城市");
		return false;
	};
	if(!checkMaxLength($comForm.find("input[name='cfeComArea']"),8)){
		layer.msg("所在城市不能超过8位");
		return false;
	};
	
	//企业类型
	if(!checkNull($comForm.find("input[name='cfeComKind']"))){
		layer.msg("请输入企业类型");
		return false;
	};
	
	//法人
	if(!checkNull($comForm.find("input[name='cfeComOperName']"))){
		layer.msg("请输入法人");
		return false;
	};
	
	//成立日期
	if(!checkNull($comForm.find("input[name='cfeComEstablish']"))){
		layer.msg("请选择成立日期");
		return false;
	};
	
	//经营范围
	if(!checkNull($comForm.find("textarea[name='cfeComScope']"))){
		layer.msg("请输入经营范围");
		return false;
	};
	return true;		
}

//验证是否有资质
function checkCertifiate(){
	var trLen = $("#certificateList tbody tr").length;
	return trLen != undefined && trLen > 0 ;
}

//验证其他信息
function checkOtherInfo(){
	var $otherForm = $("#otherForm");
	//顾问
	if(!checkNull($otherForm.find("input[name='cfeAdviserNum']"))){
		layer.msg("请选择顾问");
		return false;
	}
	
	//出售价格
	if(checkNull($otherForm.find("input[name='cfePrice']"))){
		if(!checkNumPorint($otherForm.find("input[name='cfePrice']"))){
			layer.msg("出售价格只能由整数或小数组成");
			return false;
		}
	}
	return true; 
}

//完整html标签
function checkHtml(obj){
	var objValue = $(obj).val();
	var reg = new RegExp('^<([^>\s]+)[^>]*>(.*?<\/\\1>)?$');
	if(reg.test(objValue)){
		return false;
	}
	return true;
}

//验证数字和字母
function checkNumAndLetter(obj){
	var objValue = $(obj).val();
	var reg=/^[0-9a-zA-Z]*$/g;
	if(!reg.test(objValue)){
		return false;
	}
	return true;
	
}
//验证浮点数
function checkNumPorint(obj){
	var objValue = $(obj).val();
	return /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/.test( objValue );
}

//空验证
function checkNull(obj){
	var objValue = $(obj).val();
	if(objValue == null || objValue == ""){
		return false;
	}else{
		return true;
	}
}

//验证数字
function checkNum(obj){
	var objValue = $(obj).val();
	var reg = new RegExp("^[0-9]*$");
	if(!reg.test(objValue)){
		return false;
	}else{
		return true;
	}
	return true;
}
//长度小于
function checkLength(obj,len){
	var objValue = $(obj).val();
	if(objValue.length > len){
		return false;
	}
	return true;
}

//长度等于
function checkLengthEqual(obj,len){
	var objValue = $(obj).val();
	if(objValue.length != len){
		return false;
	}
	return true;
}

//长度小于
function checkMaxLength(obj,len){
	var objValue = $(obj).val();
	if(objValue.length > len){
		return false;
	}
	return true;
}

//加载公司信息
function loadCompanyInfo(){
	var $comForm = $("#companyForm");
	//统一信用代码验证
	if(!checkNull($comForm.find("input[name='cfeComNo']"))){
		layer.msg("请输入统一信用代码");
	}; 
	if(!checkNumAndLetter($comForm.find("input[name='cfeComNo']"))){
		layer.msg("统一信用代码只能包含数字或字母");
		return false;
	};
	var regNo =$comForm.find("input[name='cfeComNo']").val(); 
	
	$.ajax({
		url:rootpath+"/certificate/getCompayInfo.shtml",
		data:{regNo:regNo},
		type:"post",
		dataType:"json",
		success:function(data){
			if(data.status){
				var obj = data.data;
				$comForm.find("input[name='cfeComName']").val($.trim(obj.name));
				$comForm.find("input[name='cfeComRegCap']").val($.trim(obj.formategistCapi));
				$comForm.find("input[name='cfeComAddress']").val($.trim(obj.address));
				$comForm.find("input[name='cfeComKind']").val($.trim(obj.econKind));
				$comForm.find("input[name='cfeComOperName']").val($.trim(obj.operName));
				$comForm.find("input[name='cfeComEstablish']").val($.trim(obj.startDate));
				$comForm.find("textarea[name='cfeComScope']").text(obj.scope);
			}else{
				$comForm.find("input[name='cfeComNo']").val("");
				layer.alert(data.info);
			}
		}
	});
}



//顾问弹窗开始 
function getFromData(formId){
	var data = {};
	$("#"+formId).serializeArray().map(function(x){data[x.name] = x.value;});
	return data;
}
function getPager(offset,url){
	var queryparams=getFromData("searchForm");
	queryparams.curIndex=offset;
	queryparams.requestMethod="getPager";
	queryparams.begin = $("#pageBegin").val();
	queryparams.end = $("#pageEnd").val();
	queryparams.category='certificate_bus_id';
	var url =  rootpath+"/adviser/findAdviserDate.shtml";
	$.post(url,queryparams,function(data){
		$("#adviser_conten_wj").html(data);
	},'html');
}
//顾问初始化方法
getPager(0);
//顾问搜索方法
function searchIntro(){
	getPager(0);	
}
//顾问重置搜索条件
function resetIntro(){
	$("input[name='adviserCond']").val('');
	getPager(0);
}
var choice_adviser_wj_id=0;
function OpenChoiceAdviser(){
	choice_adviser_wj_id= layer.open({
	  title:"搜索顾问",
	  type:1,
	  area: ['800px', '600x'],
	  content: $('#choice_adviser_wj'),
	  end:function() { }
   });
}
function closChoiceAdviser(){
	layer.close(choice_adviser_wj_id);
}
/*选择顾问  */
function choice(number,name){
	$(".relationAdviser").find("input[name='cfeAdviserNum']").val(number)
	$(".relationAdviser").find("input[name='cfeAdviserName']").val(name);
	layer.msg("选择成功！");
	closChoiceAdviser();
}
//顾问弹窗结束